package org.firebug.spring.boot.mybatis.plus.config;

import com.baomidou.mybatisplus.core.config.GlobalConfig;
import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.baomidou.mybatisplus.extension.plugins.OptimisticLockerInterceptor;
import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor;
import org.apache.ibatis.reflection.MetaObject;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.time.LocalDateTime;

@Configuration
//@MapperScan("com.firebug.spring.boot.mybatis.plus.mapper")
//@MapperScan("org.firebug.spring.boot.mybatis.plus.mapper")
public class MybatisPlusConfig {

    @Bean
    public GlobalConfig globalConfig() {
        MetaObjectHandler handler = new MetaObjectHandler() {
            @Override
            public void insertFill(MetaObject metaObject) {
                this.strictInsertFill(metaObject, "creater", Long.class, 8L);
            }

            @Override
            public void updateFill(MetaObject metaObject) {
                this.strictInsertFill(metaObject, "modifier", Long.class, 8L);
            }
        };
        return new GlobalConfig().setMetaObjectHandler(handler);
    }
    /**
     * 乐观锁插件: @Version
     */
    @Bean
    public OptimisticLockerInterceptor optimisticLockerInterceptor() {
        return new OptimisticLockerInterceptor();
    }

    /**
     * 分页插件
     */
    @Bean
    public PaginationInterceptor paginationInterceptor() {
        return new PaginationInterceptor();
    }
}
